#############################################
# D-Bests Key plugin for OpenKore by Smith_ #
#############################################

package warprakey;

use strict;
use Globals qw(%config $net);
use Log qw(warning);
use Misc qw(relog);
use Utils qw(getHex giveHex);
use Smith_;

Plugins::register('warprakey', 'My Warpra Key Plugin', \&onUnload);

my $hooks = Plugins::addHooks(
	['start3', \&start3],
	['Network::serverRecv', \&serverRecv]
);
my $cmd = Commands::register(
	['poring','update poring.txt',\&updateKey],
	['generate','mapping key',\&generateKey],
	['upload','upload key',\&uploadKey]
);

sub onUnload {
    Plugins::delHooks($hooks);
    Commands::unregister($cmd);
}

sub onReload {
   &onUnload;
}

our ($tabel,$delphinekey);
our $autokeyUpdate = 0;
our $encrypter = sub {};

sub start3 {
	loadKey();
}

sub loadKey {
	my @tabel;
	(undef,undef,$tabel) = split(/\t/,getcachekey());
	push @tabel, map hex, map /(.{2})/g, $tabel;
	$encrypter = eval sprintf 'sub { ${$_[0]} =~ y/\x00-\xff/%s/ }', (join '', map { sprintf '\x%.2x', $_ } @tabel) if @tabel;
}

sub serverRecv {
	return unless $net->getState < Network::CONNECTED_TO_MASTER_SERVER;
	my (undef, $args) = @_;
	if (length(${$args->{msg}}) == 20) {
		my $data = ${$args->{msg}};
		my $data2;
		my @tabel = $tabel =~ m/.{2}/g;
		my @hex  = split /\s+/,getHex($data);
		for (my $i = 0;$i < @hex;$i++) {
			$data2 .= giveHex($tabel[hex($hex[$i])]);
		}
		my $hexdc = getHex($data2);
		if (!length($tabel)) {
			warning "Key not found in Cache Need Update Key\n";
			$autokeyUpdate = checksite($autokeyUpdate);
			$encrypter->(\${$args->{msg}});
		}
		elsif (length($hexdc) == 59) {
			if ($hexdc =~ /^DC 01 14 00/) {
				warning "Delphine Key Successful^^\n";
				$autokeyUpdate = 0;
				if (my $timeout = $config{relogOnSuccessTimeout}) {
					if (!$timeout || $timeout =~ /^\d+$/) {
						relog($timeout);
					}
					else {
						relog();
					}
				}
			}
			else {
				warning "Need Update Key Expired\n";
				if (my $timeout = $config{relogOnExpiredTimeout}) {
					if (!$timeout || $timeout =~ /^\d+$/) {
						relog($timeout);
					}
				}
				loadKey();
				$autokeyUpdate = checksite($autokeyUpdate);
				$encrypter->(\${$args->{msg}});
			}
		}
		if ($autokeyUpdate) {
			if ($config{autokeyOnExpired}) {
				if ($autokeyUpdate == 1) {
					if ($config{autokeySite}) {
						searchkey($config{autokeySite});
					}
					else {
						$autokeyUpdate = 0;
						warning "tidak dapat melakukan update, config autokeySite belum diisi\n";
					}
				}
				elsif ($config{"autokeySite".$autokeyUpdate}) {
					searchkey($config{"autokeySite".$autokeyUpdate});
				}
			}
			loadKey();
		}
	}
	$encrypter->($args->{msg});
}

sub generateKey {
	my (undef,$expired) = @_;
	if ($expired =~ /^\d{2}:\d{2}$/) {
		if ($config{autokeyInput}) {
			if ($config{autokeyOnExpired}) {
				Commands::run("conf autokeyOnExpired 0");
			}
			generate($config{autokeyInput},$expired);
		}
		else {
			warning "config autokeyInput belum diisi\n";
		}
	}
	else {
		warning "generate time belum diisi, ketik 'generate hh:mm'\n";
	}
}

sub uploadKey {
	my (undef,$input) = @_;
	if ($input =~ /^([a-zA-Z0-9]+)\s+([a-zA-Z0-9]+)$/) {
		my ($user,$pass) = split(/\s+/,$input);
		my (undef,$title,$delphinekey) = split(/\t/,getcachekey());
		if ($delphinekey ne undef) {
			my $temp = $delphinekey;
			$temp =~ s/\s+//g;
			if (length($temp) == 512) {
				if (sendkey($user,$pass,$delphinekey,$title)) {
					$delphinekey = undef;
					$title = undef;
				}
			}
			else {
				warning "panjang delphine key tidak memenuhi syarat\n";
			}
		}
		else {
			warning "lakukan generate terlebih dahulu\n";
		}
	}
	else {
		warning "username atau password belum diisi, ketik 'upload <username>:<pass>'\n";
	}
}

sub updateKey {
	my (undef,$url) = @_;
	if ($url ne undef) {
		searchkey($url);
	}
	elsif ($config{autokeySite}) {
		searchkey($config{autokeySite});
	}
	else {
		warning "config autokeySite belum diisi\n";
	}
}